package com.vinjogames.ui;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.PointF;

import com.vinjogames.gfx.Font;
import com.vinjogames.gfx.Image;
import com.vinjogames.gfx.Shapes;
import com.vinjogames.util.GameConVars;

public class PressableButton {

	protected PointF botLeft;
	protected PointF topRight;
	protected PointF mBackgroundDrawLocation;
	protected PointF mTextDrawLocation;

	protected Image mBackground;
	protected Image mImage;
	protected String mText;
	protected int mImageNum;
	protected boolean mPressed;
	protected boolean hidden;

	protected Object data;

	static final float mHighlightR = .5f;
	static final float mHighlightG = .5f;
	static final float mHighlightB = .5f;

	public PressableButton() {
		botLeft = new PointF();
		topRight = new PointF();
	}

	public PressableButton(String text, PointF bl, PointF tr){
		mText = text;
		botLeft = bl; topRight = tr;
		mBackground = new Image(GameConVars.BUTTON, 
				topRight.x - botLeft.x, 
				topRight.y - botLeft.y);
		
		mBackground.setAnimations(new int[]{0,1,2,3});

		mBackgroundDrawLocation =  new PointF(botLeft.x + (topRight.x - botLeft.x)/2, 
				botLeft.y + (topRight.y - botLeft.y)/2);
		mTextDrawLocation =        new PointF(botLeft.x + (topRight.x - botLeft.x)/2, //The same for now
				botLeft.y + (topRight.y - botLeft.y)/2);
	}

	public PressableButton(Image image, int imageNum, PointF bl, PointF tr, boolean drawBackground) {
		botLeft = bl; topRight = tr;
		mImage = image;
		mImageNum = imageNum;
		if(drawBackground){
			mBackground = new Image(GameConVars.BUTTON,
					topRight.x - botLeft.x, 
					topRight.y - botLeft.y);
			
			mBackground.setAnimations(new int[]{0,1,2,3});
		}

		mBackgroundDrawLocation =  new PointF(botLeft.x + (topRight.x - botLeft.x)/2, 
				botLeft.y + (topRight.y - botLeft.y)/2);
		
		mTextDrawLocation =        new PointF(botLeft.x + (topRight.x - botLeft.x)/2, //The same for now
				botLeft.y + (topRight.y - botLeft.y)/2);
	}

	//Getters and setters
	public void setText(String s) { mText = s; }
	public String getText() { return mText; }
	public void setBotLeft(PointF p) { botLeft = p; }
	public void setBotLeft(float x, float y) {botLeft.set(x,y);}
	public PointF getBotLeft() { return botLeft; }
	public void setTopRight(PointF p) { topRight = p; }
	public void setTopRight(float x, float y) { topRight.set(x,y); }
	public PointF getTopRight() { return topRight; }
	public int getmImageNum() {	return mImageNum; }
	public void setmImageNum(int mImageNum) { this.mImageNum = mImageNum; }
	public Object getData(){ return data; }
	public void setData(Object o) { data = o; }

	public boolean insideBox(PointF p) {
		if(p.x >= botLeft.x && p.y >= botLeft.y & p.x <= topRight.x && p.y <= topRight.y){
			mPressed = true;
			return true;
		}
		else{
			return false;
		}
	}	

	public void draw(GL10 gl){
		if(hidden) return;
		if(mBackground != null){
			if(mPressed){
				mPressed = false;
				mBackground.draw(gl, mBackgroundDrawLocation.x,mBackgroundDrawLocation.y, mHighlightR,mHighlightG,mHighlightB,1);
			}else{
				mBackground.draw(gl, mBackgroundDrawLocation, 1);
			}
		}
		
		if(mImage != null){
			if(mPressed){
				mPressed = false;
				mImage.draw(gl,mBackgroundDrawLocation.x, mBackgroundDrawLocation.y , mHighlightR,mHighlightG,mHighlightB,1);
			}else{
				mImage.draw(gl, mBackgroundDrawLocation);
			}
		}
		
		if(mText != null && mTextDrawLocation != null){
			Font.drawString(gl, mText, mTextDrawLocation, .35f);
		}
	}

	public void drawCollisionBox(GL10 gl){
		Shapes.drawSquare(gl, topRight, botLeft, 0, 0, 1, 1);
	}

	public void update(float width, float height, PointF center){
		mImage.setWidth(width);
		mImage.setHeight(height);
		
		this.botLeft.set(center.x - mImage.getWidth()/2, center.y - mImage.getHeight()/2);
		this.topRight.set(center.x + mImage.getWidth()/2, center.y + mImage.getHeight()/2);
		this.mBackgroundDrawLocation.set(botLeft.x + (topRight.x - botLeft.x)/2, 
				botLeft.y + (topRight.y - botLeft.y)/2);

	}
	
	public float getHeight(){ return mImage.getWidth(); }
	public float getWidth(){ return mImage.getHeight(); }
	public void rotate(float angle){ mImage.rotate(angle); }
	public void setVisibility(boolean hidden){	this.hidden = hidden;}
	public Image getBG(){ return mBackground; }
	public Image getImage(){ return mImage; }
}
